package com.jz.community.moduleshoppingguide.nearshop.ui.adapter;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.glideConfig.GlideApp;
import com.jz.community.basecomm.utils.CharacterUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.RxTextTool;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.nearshop.bean.NearShopGoodsRight;
import com.jz.community.moduleshoppingguide.nearshop.ui.view.AddWidget;

import java.util.List;

/**
 * Created by Ani_H on 2018/3/8.
 */

public class DownstairGoodsAdapter extends BaseQuickAdapter<NearShopGoodsRight, BaseViewHolder> {


    private itemAddClick addClick;
    private IGoodsDetail goodsDetail;


    public DownstairGoodsAdapter(int layoutResId, @Nullable List<NearShopGoodsRight> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder holder, NearShopGoodsRight item) {
        int position = holder.getAdapterPosition();
        ImageView avatar = holder.getView(R.id.stair_goods_img);
        GlideApp.with(mContext)
                .load(mData.get(position).getImage())
                .placeholder(R.mipmap.image_default)
                .error(R.mipmap.image_default)
                .into(avatar);

        TextView title = holder.getView(R.id.stair_goods_name);
        title.setText(mData.get(position).getTitle());
        TextView tvPrice = holder.getView(R.id.stair_goods_price_red);
        TextView tvPriceGray = holder.getView(R.id.stair_goods_price_gray);
        TextView stairGoodsLimit = holder.getView(R.id.stair_goods_limit);
        TextView stairGoodsPresell = holder.getView(R.id.stair_goods_presell);
        TextView stairGoodsDiscount = holder.getView(R.id.stair_goods_discount);
        TextView stairGoodsBeforehand = holder.getView(R.id.stair_goods_beforehand);
        if (mData.get(position).getLimitedNum() > 0) {
            stairGoodsLimit.setVisibility(View.VISIBLE);
            stairGoodsLimit.setText("限购" + mData.get(position).getLimitedNum() + "件");
        }
        if (mData.get(position).getBuyType() == 2) {
            stairGoodsPresell.setVisibility(View.VISIBLE);
            stairGoodsPresell.setText("折扣");
        } else {
            stairGoodsPresell.setVisibility(View.GONE);
        }
        if (mData.get(position).getBuyType() == 3) {
            stairGoodsDiscount.setVisibility(View.VISIBLE);
            stairGoodsDiscount.setText("限时购");
        } else {
            stairGoodsDiscount.setVisibility(View.GONE);
        }
        if (mData.get(position).getBuyType() == 1) {
            stairGoodsBeforehand.setVisibility(View.VISIBLE);
            stairGoodsBeforehand.setText("预售");
        } else {
            stairGoodsBeforehand.setVisibility(View.GONE);
        }
        TextView type = holder.getView(R.id.stair_goods_type);
        if (position == 0) {
            //第一个是一定显示的
            type.setVisibility(View.VISIBLE);
        } else {
            //后一个与前一个对比,判断首字母是否相同，相同则隐藏
            String theLastPId = mData.get(position - 1).getParentId();
            if (mData.get(position).getParentId().equals(theLastPId)) {
                type.setVisibility(View.GONE);
            } else {
                type.setVisibility(View.VISIBLE);
            }
        }
        //已售罄
        TextView end_tv = holder.getView(R.id.stair_goods_end);
        AddWidget addWidget = holder.getView(R.id.stair_goods_view_add);
        if (mData.get(position).getStock() == 0) {
            end_tv.setVisibility(View.VISIBLE);
            addWidget.setVisibility(View.GONE);
        } else {
            end_tv.setVisibility(View.GONE);
            addWidget.setVisibility(View.VISIBLE);
            addWidget.setAddClick(new AddWidget.OnAddClick() {
                @Override
                public void onAddClick(View view, AddWidget addWidget, int count) {
                    addClick.onAddClick(view, addWidget, mData.get(position), count);
                }

                @Override
                public void onSubClick(AddWidget addWidget, int count) {
                    addClick.onSubClick(addWidget, mData.get(position), count);
                }

                @Override
                public void onNumClick() {
                    addClick.onNumClick(mData.get(position), addWidget);
                }
            });
            addWidget.setNeedSub(false);
            addWidget.setCount(mData.get(position).getCount());
        }
        if (!Preconditions.isNullOrEmpty(mData.get(position).getSkuInfos().get(0).getDiscountPrice())) {
            RxTextTool.getBuilder("").append("¥" + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(mData.get(position).getSkuInfos().get(0).getPrice()))).setStrikethrough().into(tvPriceGray);
            tvPrice.setText("¥" + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(mData.get(position).getSkuInfos().get(0).getDiscountPrice())));
        } else {
            tvPriceGray.setText("");
            tvPrice.setText("¥" + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(mData.get(position).getSkuInfos().get(0).getPrice())));
        }
        avatar.setOnClickListener(v -> {
            if (goodsDetail != null) {
                goodsDetail.showDetail(mData.get(position));
            }
        });
    }

    public void setAddClick(itemAddClick addClick) {
        this.addClick = addClick;
    }

    public interface itemAddClick {
        void onAddClick(View view, AddWidget addWidget, NearShopGoodsRight goodsRight, int count);

        void onSubClick(AddWidget addWidget, NearShopGoodsRight goodsRight, int count);

        void onNumClick(NearShopGoodsRight goodsRight, AddWidget addWidget);
    }

    public interface IGoodsDetail {
        void showDetail(NearShopGoodsRight goodsRight);
    }

    public void setGoodsDetail(IGoodsDetail goodsDetail) {
        this.goodsDetail = goodsDetail;
    }
}
